package com.example.guiguiapplicatio.fragment.FamilyArchives.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.api.Interface.Api;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FamilyarChivesRecordList;

import java.util.List;

import cn.refactor.lib.colordialog.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FamilyArchivesAddAdapter extends RecyclerView.Adapter<FamilyArchivesAddAdapter.FamilyArchivesAddViewHolder> {
    private Context mContext;
    private String mIdcard;
    private String HouseholdId;
    private Api api;

    private List<FamilyarChivesRecordList> familyarChivesRecordLists;

    public FamilyArchivesAddAdapter(List<FamilyarChivesRecordList> familyarChivesRecordLists, Context context, String Idcard, String householdId, Api api) {
        this.familyarChivesRecordLists = familyarChivesRecordLists;
        mContext = context;
        mIdcard = Idcard;
        HouseholdId = householdId;
        this.api = api;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FamilyArchivesAddViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_total_detail_family_add_item, parent, false);
        return new FamilyArchivesAddViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FamilyArchivesAddViewHolder holder, int position) {
        FamilyarChivesRecordList familyarChivesRecordList = familyarChivesRecordLists.get(position);
        holder.member_name.setText(familyarChivesRecordList.getName());
        holder.member_sex.setText(familyarChivesRecordList.getSex());
        holder.member_age.setText(Integer.toString(familyarChivesRecordList.getAge()) + "岁");
//        holder.member_relation.setText(familyarChivesRecordList.getRelationship());
        switch (familyarChivesRecordList.getRelationship()) {
            case "0":
                holder.member_relation.setText("自由人");
                break;
            case "1":
                holder.member_relation.setText("户主");
                break;
            case "2":
                holder.member_relation.setText("非户主");
                break;
            default:
                holder.member_relation.setText("其他状态");
                break;
        }
        holder.itemView.clearAnimation();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idCard = familyarChivesRecordList.getIdcard();
                // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                if (familyarChivesRecordList.getStatus() == 0 || familyarChivesRecordList.getStatus() == 2) {
                    if (familyarChivesRecordList.getRelationship().equals("0")) {
                        AlertDialog dialog = new AlertDialog.Builder(mContext)
                                .setTitle("添加成员")//设置标题
                                .setMessage("是否添加 " + familyarChivesRecordList.getName() + " 到家庭")
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();//销毁对话框
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Call<Object> objectCall;
                                        objectCall = api.addFamilyarChives(HouseholdId, idCard);
                                        objectCall.clone().enqueue(new Callback<Object>() {
                                            @Override
                                            public void onResponse(Call<Object> call, Response<Object> response) {
                                                if (response.code() == 200) {
                                                    Bundle bundle = new Bundle();
                                                    bundle.putString("idCard", mIdcard);
                                                    Navigation.findNavController(v).navigate(R.id.action_FamilyArchivesAddFragment_to_FamilyArchivesFragment, bundle);
                                                }
                                            }

                                            @Override
                                            public void onFailure(Call<Object> call, Throwable t) {

                                            }
                                        });
                                        dialog.dismiss();//销毁对话框
                                    }
                                }).create();
                        dialog.show();//显示对话框
                    } else {
                        new PromptDialog(mContext)
                                .setDialogType(PromptDialog.DIALOG_TYPE_INFO)
                                .setAnimationEnable(true)
                                .setTitleText("INFO")
                                .setContentText("不能添加自由人以外的身份")
                                .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                    @Override
                                    public void onClick(PromptDialog dialog) {
                                        dialog.dismiss();
                                    }
                                }).show();
                    }
                } else {
                    new PromptDialog(mContext)
                            .setDialogType(PromptDialog.DIALOG_TYPE_INFO)
                            .setAnimationEnable(true)
                            .setTitleText("INFO")
                            .setContentText("用户在签约中，请先解约")
                            .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                @Override
                                public void onClick(PromptDialog dialog) {
                                    dialog.dismiss();
                                }
                            }).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return familyarChivesRecordLists.size();
    }

    public static class FamilyArchivesAddViewHolder extends RecyclerView.ViewHolder {
        TextView member_name;
        TextView member_sex;
        TextView member_age;
        TextView member_relation;
        TextView member_people_type;

        public FamilyArchivesAddViewHolder(@NonNull View itemView) {
            super(itemView);
            member_name = itemView.findViewById(R.id.member_name);
            member_sex = itemView.findViewById(R.id.member_sex);
            member_age = itemView.findViewById(R.id.member_age);
            member_relation = itemView.findViewById(R.id.member_relation);
            member_people_type = itemView.findViewById(R.id.member_people_type);
        }
    }
}
